#!/usr/bin/env python3
#

import argparse

def numbers_set(nlist):
    # e.g. 1,2,4-6,20 => 1,2,4,5,6,20
    if nlist is None or len(nlist.strip()) == 0:
        return set()
    nums = []
    for n in nlist.strip().split(','):
        if '-' in n:
            b, e = n.split('-')
            nums.extend(list(range(int(b), int(e) + 1)))
        else:
            nums.append(int(n))
    return set(nums)

def args_parser():
    comment = ("A simple of parse number list.")
    parser = argparse.ArgumentParser(description = comment,
                                     formatter_class =
                                         argparse.RawTextHelpFormatter)
    parser.add_argument("-m", dest = "m", action = "store",
                        help = "memory bind nodes")
    parser.add_argument("-c", dest = "c", action = "store",
                        help = "CPU bind nodes")
    parser.add_argument("-N", dest = "N", type = int, action = "store",
                        default = 8, help = "NUMA nodes")
    parser.add_argument("extra", nargs = "*",
                        help = "extra arguments")

    return parser

def usage():
    args_parser().parse_args(["-h"])

def parse_args(args_list = None):
    args = args_parser().parse_args(args_list)
    memnodes = numbers_set(args.m)
    if len(memnodes) == 0 and not args.N is None:
        memnodes = set(range(0, args.N))
    setattr(args, "memnodes", memnodes)
    cpunodes = numbers_set(args.c)
    if len(cpunodes) == 0 and not args.N is None:
        cpunodes = set(range(0, args.N))
    setattr(args, "cpunodes", cpunodes)
    return args

args = parse_args()
print(args)

result = {}
for c in args.cpunodes:
    row = {}
    for m in args.memnodes:
        # TODO
        row[m] = str(c) + str(m)
    result[c] = row

# 表头
print("CPU\MEM", end = "")
for m in args.memnodes:
    print("\t%d" % m, end = "")
print("")

for k1, v1 in result.items():
    # 列头
    print("%d" % k1, end = "")
    for k2, v2 in v1.items():
        print("\t%s" % v2, end = "")
    print("")
